package com.byd.dms.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.byd.dms.entity.User;

import java.util.HashMap;
import java.util.Map;

/**
 * http://www.what21.com/c/view/java_3001_1470845369297.html
 */
public class DemoTest2 {
    
    public static void main(String[] args) {
        Map<String, User> userMap = new HashMap<String, User>();
        User zsUser = new User();
        zsUser.setId("zhangsan");
        zsUser.setName("张三");
        userMap.put("one", zsUser);
        // 用户二
        User lsUser = new User();
        lsUser.setId("lisi");
        lsUser.setName("李四");
        userMap.put("two", lsUser);
        
        String jsonString = JSON.toJSONString(userMap);
        System.out.println("Map to JSON -> ");
        System.out.println(jsonString);
        
        // 泛型的反序列化
        TypeReference typeRef = new TypeReference<Map<String, User>>() {
        };
        userMap = (Map<String, User>) JSON.parseObject(jsonString, typeRef);
        zsUser = userMap.get("one");
        System.out.println("> " + zsUser.getId());
        System.out.println("> " + zsUser.getName());
        lsUser = userMap.get("two");
        System.out.println(">> " + lsUser.getId());
        System.out.println(">> " + lsUser.getName());
        
        
    }
}
